package com.example.administrator.fragarmentdemotest_a.fragarment_demo.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.fragarmentdemotest_a.R;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.Bean.FeatureBean;
import com.example.administrator.fragarmentdemotest_a.fragarment_demo.dao.FeatureDao;

import java.util.Collections;
import java.util.List;

public class RvMoreAdapter extends RecyclerView.Adapter<RvMoreAdapter.BaseViewHolder>    {


    private List<FeatureBean> listClose;
    private List<FeatureBean> listOpen;
    private FeatureDao dao;

    public RvMoreAdapter(List<FeatureBean> listClose, List<FeatureBean> listOpen) {
        this.listClose = listClose;
        this.listOpen = listOpen;
        dao = new FeatureDao();
    }

    private final int TYPE_HEAD = 1;
    private final int TYPE_BEAN = 2;

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        } else if (position < 1 + listClose.size()) {
            return TYPE_BEAN;
        } else if (position == 1 + listClose.size()) {
            return TYPE_HEAD;
        } else {
            return TYPE_BEAN;
        }
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        BaseViewHolder vh = null;
        switch (viewType) {
            case TYPE_HEAD:
                View vHead = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_news_more_head, parent, false);
                vh = new HeadViewHolder(vHead);
                break;
            case TYPE_BEAN:
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_news_more, parent, false);
                vh = new BeanViewHolder(view);
                break;
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        if (holder instanceof BeanViewHolder) {
            BeanViewHolder vh = (BeanViewHolder) holder;
            if (position < 1 + listClose.size()) {
                FeatureBean bean = listClose.get(position - 1);
                vh.setData(bean);
            } else {
                FeatureBean bean = listOpen.get(position - listClose.size() - 2);
                vh.setData(bean);
            }
        } else if (holder instanceof HeadViewHolder) {
            HeadViewHolder vh = (HeadViewHolder) holder;
            vh.setData(position);
        }
    }

    @Override
    public int getItemCount() {
        return listClose.size() + listOpen.size() + 2;
    }


    public class BaseViewHolder extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }
    }

    public class HeadViewHolder extends BaseViewHolder {
        private TextView tv;

        public HeadViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }

        public void setData(int position) {
            tv.setText(position == 0 ? "已关闭的功能" : "已开启的功能");
        }

    }


    public class BeanViewHolder extends BaseViewHolder implements View.OnClickListener {
        private ImageView iv;
        private TextView tv;
        private Button bt;
        private FeatureBean bean;

        public BeanViewHolder(View itemView) {
            super(itemView);
          ////////////////////////////////////////
            iv = itemView.findViewById(R.id.iv);
            tv = itemView.findViewById(R.id.tv);
            bt = itemView.findViewById(R.id.bt);

            bt.setOnClickListener(this);
        }


        public void setData(FeatureBean bean) {
            this.bean = bean;
            // ~
            tv.setText(bean.getName());
            if ("0".equals(bean.getShowType())) {
                bt.setText("开启");
            } else {
                bt.setText("关闭");
            }
        }

        @Override
        public void onClick(View v) {
            if (v == bt) {

                int from = getLayoutPosition();
                int to;
                if ("0".equals(bean.getShowType())) {
                    listOpen.add(0, listClose.remove(from - 1));
                    to = listClose.size() + 2;
                } else {
                    listClose.add(listOpen.remove(from - 2 - listClose.size()));
                    to = listClose.size() + 1;
                }
                bean.setShowType("0".equals(bean.getShowType()) ? "1" : "0");
                dao.update(bean);
                notifyItemMoved(from, to);
                notifyItemChanged(to);
            }
        }
    }
}
